package com.ody.util.code.config;

import com.odianyun.util.ResourceUtils;
import com.odianyun.util.io.FileUtils;
import com.ody.util.code.Const;
import org.springframework.beans.factory.FactoryBean;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

public class ConfigProperties implements FactoryBean<Properties> {
	private String propertiesPath;
	private Properties config;
	private Properties props;
	
	public void setPropertiesPath(String propertiesPath) {
		this.propertiesPath = propertiesPath;
	}
	String getPropertiesPath() {
		return ResourceUtils.getResourcePath(propertiesPath);
	}
	
	public static ConfigProperties buildForTheme(String theme, boolean isRunGUI) throws Exception {
		String propertiesFilePath = Const.CONFIG_PATH;
		propertiesFilePath = propertiesFilePath.replace("${theme}", theme);
		return new ConfigProperties(propertiesFilePath, isRunGUI);
	}
	
	public ConfigProperties() {}
	
	public ConfigProperties(String propertiesPath, boolean isRunGUI) throws Exception {
		this.propertiesPath = propertiesPath;
		if (isRunGUI) {
			ConfigGuider configGuider = new ConfigGuider(this); 
			configGuider.openMainFrame();
			while (! configGuider.isCompleted()) {
				Thread.sleep(500);
			}
			config = getObject();
			config.putAll(configGuider.getProps());
		}
	}

	@Override
	public Properties getObject() throws Exception {
		if (config == null) {
			config = new Properties();
			
			Properties p = getProps();
			for (String key : p.stringPropertyNames()) {
				if (key.startsWith("var.")) {
					String varKey = key.substring(key.indexOf(".", 4) + 1);
					config.setProperty(varKey, p.getProperty(key));
				} else {
					config.setProperty(key, p.getProperty(key));
				}
			}
			
			IJdbcConfig dbConfig = new MysqlJdbcConfig(config.getProperty("host"), 
					Integer.parseInt(config.getProperty("port")), 
					config.getProperty("username"), 
					config.getProperty("password"), 
					config.getProperty("db"));

			config.setProperty("jdbc.driver", dbConfig.getJdbcDriver());
			config.setProperty("jdbc.url", dbConfig.getJdbcUrl());
			config.setProperty("jdbc.username", dbConfig.getJdbcUsername());
			config.setProperty("jdbc.password", dbConfig.getJdbcPassword());
		}
		return config;
	}
	
	Properties getProps() throws Exception {
		if (props == null) {
			props = new Properties();
   
			String propPath = getPropertiesPath();
			InputStream is = ResourceUtils.getInputStream(propPath);
			try {
				props.load(is);
                
                String parent = FileUtils.getParent(propPath);
                String filename = FileUtils.getFilename(propPath);
                String meConfPath = FileUtils.appendSeparatorSuffix(parent) + Const.ME_CONFIG_FILE_PREFIX + filename;
                if (new File(meConfPath).exists()) {
                    is.close();
                    is = ResourceUtils.getInputStream(meConfPath);
                    Properties meProps = new Properties();
                    meProps.load(is);
                    for (String key : props.stringPropertyNames()) {
                        props.setProperty(key, meProps.getProperty(key, props.getProperty(key)));
                    }
                }
			} finally {
				is.close();
			}
		}
		return props;
	}

	@Override
	public Class<?> getObjectType() {
		return Properties.class;
	}
	
}
